package qimao.com.edu.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import qimao.com.edu.DTO.BookDTO;
import qimao.com.edu.entity.CommenResult;
import qimao.com.edu.entity.Page;
import qimao.com.edu.service.WorksService;
import qimao.com.edu.service.impl.WorksServiceImpl;



@WebServlet("/selectBook.do")
public class selectBookServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		
		WorksService ws = new WorksServiceImpl();
		int pageNum = 1;
		int pageSize = 10;
	    
	    
		String keyword = request.getParameter("keyword");
		try {
	    	if(Integer.parseInt(request.getParameter("pageNum"))>1) {
	    		pageNum = Integer.parseInt(request.getParameter("pageNum"));
	    	}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	    try {
	    	if(Integer.parseInt(request.getParameter("pageSize"))>5) {
	    		pageSize = Integer.parseInt(request.getParameter("pageSize"));
	    	}
		} catch (Exception e) {
			// TODO: handle exception
		}

	    Page<BookDTO> page = ws.searchWorks(keyword, pageNum, pageSize);
	    System.out.println("11");
	    CommenResult cr = new CommenResult();
		cr.setCode(200);
		cr.setMsg("success");
		cr.setData(page);
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(cr.toJsonString());
	}
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
}
